﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://CPFUI.IControl.IMList/Stylesheet1.css")]//用于设计的时候加载样式
    public class IMGroup : Control
	{
        private bool isShowHiding;

        public ListBox IMList;
        private bool _isOpen = false;
        public bool IsOpen
        {
            get { return _isOpen; }
            set
            {
                _isOpen = value;
                ShowHideInfo();
            }
        }
        public string Text
        {
            get { return GetValue<string>(); }
            set
            {
                SetValue(value);
            }
        }
        public string OffOnlineInfo
        {
            get { return GetValue<string>(); }
            set
            {
                SetValue(value);
            }
        }
        //模板定义
        protected override void InitializeComponent()
        {
            Background = Color.White;
            Width = 276;
            Height = 500;
            IMList = new ListBox()
            {
                MarginTop = 30,
                MarginLeft = 0,
                MarginRight = 0,
                MarginBottom = 0,
            };

			Children.Add(new TextBlock
            {
                Cursor = Cursors.Hand,
                Text = "我的好友",
                MarginTop = 0,
                MarginLeft = 25,
                MarginRight = 0,
                Height = 30,
                FontSize = 14,
                VerticalAlignment = VerticalAlignment.Center,
                Bindings =
                {
                    {nameof(TextBlock.Text),nameof(Text),this,BindingMode.TwoWay }
                },
                Commands =
                {
                    {
                        nameof(MouseDown),
                        (s,e)=>
                        {
                            IsOpen = !IsOpen;
                        }
                    }
                }

            });
            Children.Add(new TextBlock
            {
                Cursor = Cursors.Hand,
                Text = "1/20",
                MarginTop = 0,
                MarginRight = 10,
                Height = 30,
                FontSize=10,
                Foreground= Color.Gray,
                VerticalAlignment = VerticalAlignment.Center,
                Bindings =
                {
                    {nameof(TextBlock.Text),nameof(OffOnlineInfo),this }
                },

            });
            Children.Add(new Panel
                {
                    Cursor = Cursors.Hand,
                    Width = 12,
                    Height =6,
					MarginLeft =4,
					MarginTop = 12,
                    RenderTransform = new RotateTransform(0),
                    Tag = Visibility.Visible,
                    Name = "PnlOpenClose",
                    PresenterFor = this,
                    Children =
                    {
                        new Line
                        {
                            Width = 12,
                            Height = 6,
                            StartPoint = new Point(0, 0),
                            EndPoint = new Point(6, 6),
                            StrokeStyle = "1",
                            IsAntiAlias = true,
                            Classes =
                            {
                                "BaclStrokeFill"
                            }
                        },
                        new Line
                        {
                            Width = 12,
                            Height = 6,
                            StartPoint = new Point(6, 6),
                            EndPoint = new Point(12, 0),
                            StrokeStyle = "1",
                            IsAntiAlias = true,
                            Classes =
                            {
                                "BaclStrokeFill"
                            }
                        }
                    },
                    Commands =
                    {
                        {
                            nameof(MouseDown),
                            (s,e)=>
                            {
                                IsOpen = !IsOpen;
                            }
                        }
                    }
                }
            );
            Children.Add(IMList);
		}

        public void AddItem(IMListBoxItem item)
        {
            IMList.Items.Add(item);
            OffOnlineInfo = string.Format("{0}/{1}",0,IMList.Items.Count);
            ShowHideInfo();
        }
		public void ShowHideInfo()
        {
            Panel pnl = FindPresenterByName<Panel>("PnlOpenClose");
			int h = 30;
            float height = 0;
            if (IsOpen)
            {
                for (int i = 0; i < IMList.Items.Count; i++)
                {
                    IMListBoxItem item = IMList.Items[i] as IMListBoxItem;
                    height += item.Height.Value;
                }
                height += 50f;
            }
            else
            {
                height = 30;
            }
            
            if (isShowHiding)
			{
				return;
			}
			isShowHiding = true;
			Thread thread = new Thread(delegate (object? a)
			{
				Panel panel = a as Panel;
				switch (IsOpen)
				{
					case true:
						{
							float num2 = ((RotateTransform)panel.RenderTransform).Angle;
							while (num2 < 0f)
							{
								num2 += 5f;
								pnl.RenderTransform = new RotateTransform(num2);
								Thread.Sleep(1);
							}
							pnl.RenderTransform = new RotateTransform(0f);
							isShowHiding = false;
							break;
						}
					case false:
						{
							float num = ((RotateTransform)panel.RenderTransform).Angle;
							while (num > -90f)
							{
								num -= 5f;
								pnl.RenderTransform = new RotateTransform(num);
								Thread.Sleep(1);
							}
							isShowHiding = false;
							pnl.RenderTransform = new RotateTransform(-90f);
							break;
						}
				}
			});
			thread.IsBackground = true;
			thread.Start(pnl);
			if (!IsOpen)
			{
				IMList.Visibility= Visibility.Hidden;
                this.TransitionValue("Height", (FloatField)height, TimeSpan.FromSeconds(0.2), (IEase)new PowerEase(), AnimateMode.EaseIn, (Action)delegate
				{
					this.Height = h;
				}, 1u);
			}
			if (!IsOpen)
			{
				return;
			}
            this.TransitionValue("Height", (FloatField)height, TimeSpan.FromSeconds(0.2), (IEase)new PowerEase(), AnimateMode.EaseIn, (Action)delegate
			{
				this.Height = height;
                IMList.Visibility = Visibility.Visible;
            }, 1u);
		}
	}
}
